﻿//===============================================================================
// PostSharp Poco implementation for EntityFramework
//===============================================================================
// Copyright © Ruurd Boeke.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using EditableBusinessObjects.Postsharp.Public;

namespace EntityFrameworkContrib.PostSharp4EF
{
    /// <summary>
    /// Implementation of the <see cref="INotifyPropertyChanged"/> interface.
    /// </summary>
    [Serializable]
    class NotifyPropertyChangedImplementation : INotifyPropertyChanged, IFirePropertyChanged
    {
        // Instance that exposes the current implementation.
        private readonly object instance;

        /// <summary>
        /// Initializes a new <see cref="NotifyPropertyChangedImplementation"/> instance.
        /// </summary>
        /// <param name="instance">Instance that exposes the current implementation.</param>
        public NotifyPropertyChangedImplementation(object instance)
        {
            this.instance = instance;
        }

        /// <summary>
        /// Event raised when a property is changed on the instance that
        /// exposes the current implementation.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the <see cref="PropertyChanged"/> event. Called by the
        /// property-level aspect (<see cref="AddNotifyPropertyChangedInterfaceSubAspect"/>)
        /// at the end of property set accessors.
        /// </summary>
        /// <param name="propertyName">Name of the changed property.</param>
        public void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this.instance, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
